using System;
using System.Data;
using System.Windows.Forms;
using Company.BLL;
using Company.BLL.GC;
using Company.BLL.DuLieuChuan;
using Janus.Windows.GridEX;
using Company.BLL.KDT;

namespace Company.Interface.GC
{
    public partial class DinhMucGCRegistedForm : BaseForm
    {        
        public bool IsBrowseForm = false;
        DinhMuc dinhmuc = new DinhMuc();
        public DinhMucGCRegistedForm()
        {
            InitializeComponent();
        }

        public void BindData()
        {            
            string where = string.Format("MaDoanhNghiep = '{0}' ", GlobalSettings.MA_DON_VI);
            if (ctrDonViHaiQuan.Ma != "")
                where += string.Format(" and mahaiquan = '{0}' ", ctrDonViHaiQuan.Ma);    
            if (cbHopDong.Text != "")
                where += string.Format(" and sohopdong = '{0}' ", cbHopDong.Text);
            Company.BLL.GC.DinhMuc dm = new DinhMuc();
            dgList.DataSource = dm.SelectCollectionDynamic(where, "");                 
        }
        private void BindHopDong()
        {
            string maHQ = ctrDonViHaiQuan.Ma;
            Company.BLL.GC.HopDong hopdong = new Company.BLL.GC.HopDong();
            DataTable dt;
            if (maHQ.Length > 0)
            {
                string where = string.Format("[MaHaiQuan] LIKE '%{0}%'", maHQ);
                dt = hopdong.SelectDynamic(where, "").Tables[0];
            }
            else
            {
                dt = hopdong.SelectAll().Tables[0];
            }
            cbHopDong.DataSource = dt;
            cbHopDong.DisplayMember = "SoHopDong";
            cbHopDong.ValueMember = "SoHopDong";
            try
            {
                cbHopDong.SelectedIndex = 0;
            }
            catch { }
        }
        //-----------------------------------------------------------------------------------------
        
        private void khoitao_DuLieuChuan()
        {
            // Đơn vị tính.
            this._DonViTinh = DonViTinh.SelectAll();

            // Đơn vị Hải quan.            
        }

        //-----------------------------------------------------------------------------------------

        private void DinhMucGCRegistedForm_Load(object sender, EventArgs e)
        {
            this.khoitao_DuLieuChuan();            
            BindHopDong();
            BindData();
        }

        //-----------------------------------------------------------------------------------------
        
        private void btnGetListFromHQ_Click(object sender, EventArgs e)
        {
            WSForm wsForm = new WSForm();
            wsForm.ShowDialog(this);
            if (!wsForm.IsReady) return;
            try
            {
                this.Cursor = Cursors.WaitCursor;
                if (cbHopDong.Text.Trim().Length > 0)
                {
                    DateTime ngayky=Convert.ToDateTime(cbHopDong.DropDownList.GetRow().Cells["NgayKy"].Text);
                    DinhMuc.DongBoHaiQuan(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI, cbHopDong.Text, ngayky);
                }
                else
                    DinhMuc.DongBoHaiQuan(ctrDonViHaiQuan.Ma, GlobalSettings.MA_DON_VI);
                BindData();
                this.Cursor = Cursors.Default;
                ShowMessage("Đã cập nhật thành công", false);
            }
            catch (Exception ex)
            {
                this.Cursor = Cursors.Default;
                if (ShowMessage("Có lỗi khi thực hiện. Bạn có muốn đưa vào hàng đợi không" + ex.Message, true) == "Yes")
                {
                    HangDoi hd = new HangDoi();
                    hd.ID = 0;
                    hd.MaHaiQuan = ctrDonViHaiQuan.Ma;
                    hd.SoHopDong=cbHopDong.Text;
                    if (cbHopDong.Text.Trim().Length > 0)
                    {
                        DateTime ngayky = Convert.ToDateTime(cbHopDong.DropDownList.GetRow().Cells["NgayKy"].Text);
                        hd.NgayKy = ngayky;
                    }                   
                    hd.LoaiToKhai = LoaiToKhai.DINH_MUC;
                    hd.TrangThai = 1;
                    hd.ChucNang = ChucNang.NHAN_THONG_TIN;
                    MainForm.AddToQueueForm(hd);
                    MainForm.ShowQueueForm();
                }
            }       


        }

        //-----------------------------------------------------------------------------------------
        
        private void dgList_RowDoubleClick(object sender, RowActionEventArgs e)
        {
            if (e.Row.RowType == RowType.Record)
            {
                string soHD = cbHopDong.Text;
                string maHQ = ctrDonViHaiQuan.Ma;
                string maDN = GlobalSettings.MA_DON_VI;

                DateTime ngayKy = Convert.ToDateTime(cbHopDong.DropDownList.GetRow().Cells["NgayKy"].Text);
                dinhmuc = new DinhMuc();
                this.dinhmuc.SoHopDong = soHD;
                this.dinhmuc.MaHaiQuan = maHQ;
                this.dinhmuc.MaDoanhNghiep = maDN;
                this.dinhmuc.NgayKy = ngayKy;
                this.dinhmuc.MaSanPham = e.Row.Cells[1].Text;
                this.dinhmuc.MaNguyenPhuLieu = e.Row.Cells[2].Text;
                this.dinhmuc.DinhMucSuDung = Convert.ToDecimal(e.Row.Cells[3].Text);
                this.dinhmuc.TyLeHaoHut = Convert.ToDecimal(e.Row.Cells[4].Text);
                this.dinhmuc.NPL_TuCungUng = Convert.ToDecimal(e.Row.Cells[5].Text);
                this.dinhmuc.GhiChu = (e.Row.Cells[6].Text);
                dinhmuc.Load();
                DinhMucGCRegistedDetailForm f = new DinhMucGCRegistedDetailForm();
                f.DMDetail = dinhmuc;
                f.ShowDialog();
            }
        }

        private void ctrDonViHaiQuan_ValueChanged(object sender, EventArgs e)
        {
            BindHopDong();
            this.BindData();
           
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void dgList_LoadingRow(object sender, RowLoadEventArgs e)
        {
           
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            BindData();
        }

        //-----------------------------------------------------------------------------------------
    }
}